package com.mine.frame.common.net.utils

import com.blankj.utilcode.util.ToastUtils
import retrofit2.HttpException
import java.lang.RuntimeException
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import java.net.UnknownServiceException

/**
 * company 广东玛格家居有限公司
 * @author smoker
 * @Date 2020/11/10
 * Description:请求错误提示
 */
class NetErrorUtils {

    companion object {
        fun showError(e: Throwable) {
            showError(
                e,
                "网络开小差了，请稍后重试"
            )
        }

        fun showError(e: Throwable, defMsg: String) {
            when (e) {
                is RuntimeException -> {
                    ToastUtils.showShort("运行异常")
                }
                is SocketTimeoutException -> {
                    ToastUtils.showShort("请求超时")
                }
                is ConnectException -> {
                    ToastUtils.showShort("请检查网络")
                }
                is UnknownHostException,
                is UnknownServiceException -> {
                    ToastUtils.showShort("连接服务器失败")
                }
                is HttpException -> {
                    when (e.code()) {
                        400 -> {
                            ToastUtils.showShort("${e.code()}:请求错误")
                        }
                        403 -> {
                            ToastUtils.showShort("${e.code()}:服务器拒绝请求")
                        }
                        404 -> {
                            ToastUtils.showShort("${e.code()}:请求不存在")
                        }
                        408 -> {
                            ToastUtils.showShort("${e.code()}:网络开小差了，请稍后重试")
                        }
                        500 -> {
                            ToastUtils.showShort("${e.code()}:服务器正在维护中,请稍后重试")
                        }
                        503 -> {
                            ToastUtils.showShort("${e.code()}:服务器忙，请稍后重试")
                        }
                        504 -> {
                            ToastUtils.showShort("${e.code()}:网络开小差了，请稍后重试")
                        }
                        else -> {
                            ToastUtils.showShort(defMsg)
                        }
                    }
                }
                else -> {
                    ToastUtils.showShort(defMsg)
                }
            }
        }
    }


}